package com.hopu.springbootdemo2.service.impl;

import com.hopu.springbootdemo2.entity.User;
import com.hopu.springbootdemo2.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 用户登录验证业务逻辑实现类
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //按用户名查询用户
        User user = userMapper.selectByUsername(username);
        if(user == null){
            throw new UsernameNotFoundException("username is not exists");
        }
        //包装用户信息 1)用户名 2）密码 3）权限列表
        UserDetails userDetails = new org.springframework.security.core.userdetails.User(
                user.getUserName(),
                user.getUserPwd(),
                AuthorityUtils.commaSeparatedStringToAuthorityList("")
        );
        return userDetails;
    }
}
